package service;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import entity.Admin;
import entity.Score;
import entity.Student;
import entity.Teacher;
import entity.TeacherSubject;
import factory.CommonServiceFactory;
import factory.UserModelFactory;
import mapper.ScoreMapper;
import mapper.TeacherMapper;
import mapper.TeacherSubjectMapper;
import model.UserModel;

@Service
public class TeacherService {

	private UserModel teacherModel;
	private CommonService teacherCommonService;
	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private TeacherSubjectMapper teacherSubjectMapper;
	@Autowired
	private ScoreMapper scoreMapper;
	public TeacherService() {
		teacherCommonService  = CommonServiceFactory.create(CommonServiceFactory.TEACHER);
		
	}
	public Teacher login(Teacher teacher,HttpSession session) {
		if(teacherCommonService.checkPassword(teacher.getUsername(), teacher.getPassword(),teacherMapper)) {
			Teacher cteacher = teacherMapper.findByUsername(teacher.getUsername());
			session.setAttribute("teacher", cteacher);
			return cteacher;
			
		}else {
			return null;
		}
	}
	
	public ArrayList<Score> findScore(HttpSession session){
		return teacherCommonService.queryScores(session,teacherMapper);
	}
	public ArrayList<Teacher> findAllTeacher(HttpSession session){
	   	Admin admin = (Admin)session.getAttribute("admin");
	   	if(admin!=null) {
	   		return teacherMapper.findAll();
	   	}else {
	   		return null;
	   	}
	}
	public String add(Teacher teacher,HttpSession session) {
		teacherModel = UserModelFactory.cteate(session, teacher,teacherMapper);
		return teacherModel.doAdd();
	}
	public String delete(Teacher teacher,HttpSession session) {
		teacherModel = UserModelFactory.cteate(session, teacher,teacherMapper);
		return teacherModel.doDelete();
	}
	public String alter(Teacher teacher,HttpSession session) {
		teacherModel = UserModelFactory.cteate(session, teacher,teacherMapper);
		return teacherModel.doAlter();
	}
	public Teacher findById(String id) {
		return teacherMapper.findById(id);
	}
	public ArrayList<TeacherSubject> teacherSubjects(HttpSession session){
		Teacher teacher = (Teacher)session.getAttribute("teacher");
		if(teacher!=null) {
			return teacherSubjectMapper.findByTeacherId(teacher.getId());
		}
		return null;
	}
	public String addScore(String[] studentId,double[] fraction,String teacherSubjectId) {
		TeacherSubject teacherSubject = new TeacherSubject();
		teacherSubject.setId(teacherSubjectId);
		teacherSubject.setFlag("1");
		Score score = new Score();
		score.setTeacherSubject(teacherSubject);
		
		for(int i=0;i<studentId.length;i++) {
			Student student = new Student();
			student.setId(studentId[i]);
			score.setStudent(student);
			score.setFraction(fraction[i]);
			scoreMapper.add(score);
		}
		teacherSubjectMapper.updateFlag(teacherSubject);
		return "添加成功";
	}
	public ArrayList<Score> findScoresByTeacherSubjcet(String id){
		return scoreMapper.findByTeacherSubjectId(id);
	}
}
